<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="userName">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.userName"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
  import { User, Lock } from '@element-plus/icons-vue'
  import { reactive, ref } from 'vue'
  import { useRouter, useRoute } from 'vue-router'
  import { ElNotification } from 'element-plus'
  //引入用户相关的小仓库
  import useUserStore from '@/store/modules/user'
  //引入获取当前时间段的函数
  import { getTime } from '@/utils/time'
  let useStore = useUserStore()
  //获取el-form组件
  let loginForms = ref()
  //获取路由器
  let $router = useRouter()
  //获取路由对象
  let $route = useRoute()
  //定义变量控制按钮加载状态
  let loading = ref(false)
  //收集账号与密码数据
  let loginForm = reactive({ userName: 'admin', password: '111111' })
  const login = async () => {
    //保证表单项全部校验通过后再发请求
    await loginForms.value.validate()

    loading.value = true
    //点击登录按钮以后要干什么？
    //通知仓库发登录请求
    //请求成功->首页
    //请求失败->弹出提示信息
    try {
      //可以书写.then语法
      //保证登录成功
      await useStore.userLogin(loginForm)
      const redirect: any = $route.query.redirect
      //编程式导航跳转
      $router.push(redirect || './')
      //登录成功提示
      ElNotification({
        type: 'success',
        message: '登录成功',
        title: `Hi, ${getTime()}好`,
      })
      loading.value = false
    } catch (error) {
      loading.value = false
      //登录失败提示
      ElNotification({
        type: 'error',
        message: (error as Error).message,
      })
    }
  }
  //自定义校验规则
  const checkUsername = (rule: any, value: any, callback: any) => {
    if (value.length >= 5 && value.length <= 10) {
      callback()
    } else {
      callback(new Error('账号长度在5-10位之间'))
    }
  }

  const validatePass = (rule: any, value: any, callback: any) => {
    if (value.length >= 5 && value.length <= 15) {
      callback()
    } else {
      callback(new Error('密码长度在5-15位之间'))
    }
  }
  //定义表单校验需要的配置对象
  const rules = {
    userName: [{ required: true, trigger: 'change', validator: checkUsername }],
    password: [{ required: true, trigger: 'change', validator: validatePass }],
  }
</script>

<style scoped lang="scss">
  .login_container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/login_background.jpg') no-repeat;
    background-size: cover;
    .login_form {
      position: relative;
      width: 80%;
      top: 30vh;
      background: url('@/assets/images/login_form_background.jpg') no-repeat;
      background-size: cover;
      padding: 40px;
      border-radius: 20px;
      h1 {
        color: white;
        font-size: 40px;
      }
      h2 {
        color: white;
        font-size: 20px;
        margin: 20px 0px;
      }
    }
    .login_btn {
      width: 100%;
    }
  }
</style>
